//===============================================================================
// Microsoft patterns & practices
// Web Client Software Factory
//===============================================================================
// Copyright � Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using System.Web.UI;

using Microsoft.Practices.ObjectBuilder;
using Page = Microsoft.Practices.CompositeWeb.Web.UI.Page;

using MVPComposibility.ContactManager.Views;

public partial class AjaxContactsEventBroker : Page, IDefaultView
{
	private DefaultViewPresenter _presenter;

	protected void Page_Load(object sender, EventArgs e)
	{
		if (!IsPostBack)
		{
			_presenter.OnViewInitialized();
		}
		_presenter.OnViewLoaded();

		contactList.Changed += Update;
		ContactDetails.Changed += Update;
	}

	/// <summary>
	/// Handlers to explicitly call update on the UpdatePanel which contains 
	/// </summary>
	/// <param name="sender">A Control</param>
	/// <param name="e">Who cares?</param>
	protected void Update(object sender, EventArgs e)
	{
		Control c = (Control)sender;
		while (c.Parent != null)
		{
			c = c.Parent;
			UpdatePanel up = c as UpdatePanel;
			if (up != null)
			{
				up.Update();
				return;
			}
		}
	}

	[CreateNew]
	public DefaultViewPresenter Presenter
	{
		set
		{
			_presenter = value;
			_presenter.View = this;
		}
	}
}